// pages/menu/menu.js
var call = require("../../utils/request.js")
Page({
  data: {
    swiperTitle: [{
      text: "点菜",
      id: 1
    }, {
      text: "商家",
      id: 2
    }],
    height: 0,
    shopId:"",
    shopImg: "",
    shopName: "",
    shopAddr: "",
    shopPhone: "",
    shopTime: "",
    startTime: "",
    endTime: "",
    lat: "",
    long: "",
    menu: [], //菜单
    shopCar: [], //购物对象
    cost: 0, //总价
    menuNumber: 0, //数量
    selected: 0
  },
  exteralClasses: ['i-class'],
  pullBar: function() {
    this.setData({
      pullBar: !this.data.pullBar
    })
  },
  //添加商品
  addToTrolley: function(e) {
    var info = this.data.menu;
    this.data.shopCar.push(info[this.data.selected].fs[e.currentTarget.dataset.index]);
    info[this.data.selected].fs[e.currentTarget.dataset.index].orderCount++;
    this.data.menuPrice = info[this.data.selected].fs[e.currentTarget.dataset.index].price;

    
    this.setData({
      menuNumber: this.data.shopCar.length,
      cost: (this.data.cost * 1000 + this.data.menuPrice * 1000) / 1000,
      menu: info,
    })
    console.log(this.data.shopCar);
  },
  //删除商品
  removeFromTrolley: function(e) {
    var info = this.data.menu;
    info[this.data.selected].fs[e.currentTarget.dataset.index].orderCount--;
    for (var i = 0; i < this.data.shopCar.length;i++){
      if (info[this.data.selected].fs[e.currentTarget.dataset.index] == this.data.shopCar[i]){
        var deleteIndex= i;
        this.data.shopCar.splice(i,1);
      }
    } 
    this.data.menuPrice = info[this.data.selected].fs[e.currentTarget.dataset.index].price;
    this.setData({
      cost: (this.data.cost * 1000 - this.data.menuPrice * 1000) / 1000,
      menuNumber: this.data.shopCar.length,
      menu: info,
    })
    console.log(this.data.shopCar);
  },
  turnMenu: function(e) {
    this.setData({
      selected: e.currentTarget.dataset.index
    })
  },
  //结算
  //把购物车商品和总额传递到点餐详情页
  settlement() {
    if(this.data.cost==0){
      return
    }else{
      let array = Array.from(new Set(this.data.shopCar));
      console.log(array);
      this.data.shopCar = array;
      wx.navigateTo({
        url: '../orderDetails/orderDetails?shopid=' + this.data.shopId + '&shopName=' + this.data.shopName + '&cost=' + this.data.cost + '&list=' + JSON.stringify(this.data.shopCar)
      })
    }
  },
  
  onLoad: function(options) {
    var that = this;
    //console.log(options);
    that.setData({
      shopId: options.id,
      shopImg: options.img,
      shopName: options.name,
      shopAddress: options.address,
      shopPhone: options.phone,
      shopTime: options.time,
      startTime: (options.startTime).substr(11, 5),
      endTime: (options.endTime).substr(11, 5),
      lat: options.lat,
      long: options.long,
    })
    call.getData('shop/allFoodByShop', {
      shopId: options.id
    }, this.getFood, this.failAdv);
    this.getHeight();
  },
  //获取商品
  getFood: function(data) {
    var that = this;
    that.setData({
      menu: data,
    })
  },
  //跳转到商家地图
  onMap: function() {
    wx.navigateTo({
      url: '../map/map?lat=' + this.data.lat + '&long=' + this.data.long,
    })
  },
  //获取orderPage的高度=设备高度-顶部详情-tab
  getHeight: function() {
    var that = this;
    var height = wx.getSystemInfoSync().windowHeight;
    var query = wx.createSelectorQuery();
    query.select('.item-wapper').boundingClientRect();
    query.selectViewport().scrollOffset();
    query.exec((res) => {
      var listHeight = res[0].height;
      that.setData({
        height: height - listHeight - 44
      })
    });
  }
})